package com.gisuni.sddc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONObject;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.model.Response;
import com.gisuni.sddc.service.LandStatisticsService;

/**
 * 土地拓展
 *
 */
@IocBean(singleton = false)
@At("/client/service/landStatisticsController") 
public class LandStatisticsController {
	private Log log = Logs.getLog(LandStatisticsController.class);
	@Inject
	private LandStatisticsService  landStatisticsService;

	/**
	 * 查询所有组织数据00
	 * 
	 * @return
	 */
	@At("/queryOrganizationList")
	@GET
	@POST
	@Ok("json")
	public Object queryOrganizationList(@Param("departmentType") String departmentType,@Param("projectType") String projectType,@Param("startdate") String startdate,@Param("enddate") String enddate,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = landStatisticsService.queryOrganizationList(departmentType,projectType,startdate,enddate);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	/**
	 * 查询所有数据
	 * 
	 * @return
	 */
	@At("/queryLandList")
	@GET
	@POST
	@Ok("json")
	public Object queryLandList(@Param("projectType") String projectType,@Param("startdate") String startdate,@Param("enddate") String enddate, HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
				res = landStatisticsService.queryLandList(projectType,startdate,enddate);
			
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	
	/**
	 * 查询所有数据
	 * 
	 * @return
	 */
	@At("/queryAndStatisticsLand")
	@GET
	@POST
	@Ok("json")
	public Object queryAndStatisticsLand(@Param("departmentType") String departmentType,@Param("projectType") String projectType,@Param("startdate") String startdate,@Param("enddate") String enddate, HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			if(departmentType.equals("company")){
				res = landStatisticsService.queryAndStatisticsLandByCompany(departmentType,projectType,startdate,enddate);
			}else if(departmentType.equals("department")){
				res = landStatisticsService.queryAndStatisticsLandByDepartment(departmentType,projectType,startdate,enddate);
			}else if(departmentType.equals("teamgroup")){
				res = landStatisticsService.queryAndStatisticsLandByTeam(departmentType,projectType,startdate,enddate);
			}
			
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
    
	/**
	 * 导出列表Excel
	 * @param departmentType
	 * @param projectType
	 * @param startdate
	 * @param enddate
	 * @param response
	 */
	@At("/exportlandStatisticsExcel")
	@GET
	@POST
	public void exportlandStatisticsExcel(@Param("departmentType") String departmentType,@Param("projectType") String projectType,@Param("startdate") String startdate,@Param("enddate") String enddate,HttpServletResponse response) {
		try {
			landStatisticsService.exportlandStatistics(departmentType,projectType,startdate,enddate, response);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
	}
	
	
	
	/**
	 * 根据人员组织关系表统计任务数
	 * 
	 * @return
	 */
	@At("/queryTaskTarget")
	@GET
	@POST
	@Ok("json")
	public Object queryTaskTarget( HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
//			 res = landStatisticsService.queryTaskTarget();
			 res = landStatisticsService.queryTaskTargetByOrgTarget();
			 
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}

	/**
	 * 导出外业拓展详细信息
	 */
	@At("/exportWYTZInfo")
	@GET
	@POST
	public void exportWYTZInfo(HttpServletRequest request,HttpServletResponse response){
		String ids = request.getParameter("ids");
		String tableName = request.getParameter("tableName");
		try {
			landStatisticsService.exportWYTZInfo(ids,tableName,response);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}
	
}
